<template>
  <div class="register-form-container">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="80px" class="register-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="register-btn" @click="onSubmit">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRegisterStore } from '@/stores/auth/register'
import { useUserStore } from '@/stores/auth/user'
const registerStore = useRegisterStore()
const userStore = useUserStore()
const formRef = ref(null)
const form = ref({
  username: '',
  password: '',
  confirmPassword: ''
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '用户名长度3-16位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度6-20位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: (rule: any, value: string, callback: any) => {
      if (value !== form.value.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ]
}

const onSubmit = async () => {
  // console.log('onSubmit triggered, formRef:', formRef.value)
  try {
    // Element Plus validate 支持返回 Promise，使用 await 更可靠
    await (formRef.value as any).validate()
    console.log('验证通过')
    await verify()
    // 这里可以调用后端注册接口
  } catch (err) {
    ElMessage.error('请检查表单输入')
  }
}

const emit = defineEmits<{
  (e: 'verified'): void
}>()

async function verify() {
  // console.log('verify() called')
  try{
      await registerStore.register(
        form.value.username,
        form.value.password
      )
      console.log('注册成功')
      registerStore.setUsername(form.value.username)
      registerStore.setPassword(form.value.password)
      userStore.setUser()
      userStore.setUsername(form.value.username)
      userStore.setEmail(registerStore.user.email)
      userStore.setToken(registerStore.token)
      // 通知父组件验证成功
      emit('verified')
  }
  catch(error){
      console.error('register failed', error)
      return false
  }
  return true
}
defineExpose({
  verify
})
</script>

<style scoped>
.register-form-container {
  max-width: 400px;
  margin: 60px auto;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 24px 0 rgba(0, 123, 255, 0.08);
  padding: 40px 32px 24px 32px;
  border: 1px solid #e6f0fa;
}
.register-form {
  --el-color-primary: #1976d2;
}
.el-form-item__label {
  color: #1976d2;
  font-weight: bold;
}
.register-btn {
  width: 100%;
  background: linear-gradient(90deg, #1976d2 0%, #42a5f5 100%);
  border: none;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  border-radius: 6px;
  box-shadow: 0 2px 8px 0 rgba(25, 118, 210, 0.12);
}
.register-btn:hover {
  background: linear-gradient(90deg, #1565c0 0%, #1e88e5 100%);
}
</style>
